package org.yzh;

import cn.hutool.core.util.HexUtil;
import io.github.yezhihao.protostar.util.Explain;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import org.yzh.protocol.JT808Beans;
import org.yzh.protocol.basics.JTMessage;
import org.yzh.protocol.codec.JTMessageAdapter;
import org.yzh.protocol.t808.T8300xy;

/**
 * 解码分析
 * @author yezhihao
 * https://gitee.com/yezhihao/jt808-server
 */
public class Elucidator extends JT808Beans {

    public static final JTMessageAdapter coder = new JTMessageAdapter("org.yzh.protocol");

    public static void main(String[] args) {
//        String hex = "020000d40123456789017fff000004000000080006eeb6ad02633df7013800030063200707192359642f000000400101020a0a02010a1e00640001b2070003640e200707192359000100000061646173200827111111010101652f000000410202020a0000000a1e00c8000516150006c81c20070719235900020000000064736d200827111111020202662900000042031e012c00087a23000a2c2a200707192359000300000074706d732008271111110303030067290000004304041e0190000bde31000d90382007071923590004000000006273642008271111110404049d";
        String hex = "7e010000360820376057280000002C0133313930303058595F4445564943455F5438303800000000000058595F30303100000000000000000000000000000000000000037e";
//        hex = "010000219999999991180000002a0852313233343539393939393939393939393931313801d4c1423838383838be";
//        String hex = "7e810000123333333330017318000000313233343536373839303132333435f87e";
//        hex = "7e020000730580556794320018000000800000000100000000000000000000000000002405161731580402010a0501000602004930011a310100610100640400000882650100542b060495e66a98812a08aa896df3f43d80ea077c4a9345f88c21598fa04d70af6ac719294d88c397c2ccf4505d0b0101cc00249f0d62784332f77e";
        hex = "7e020000730580556794320018000000800000000100000000000000000000000000002405161731580402010a0501000602004930011a310100542b060495e66a98812a08aa896df3f43d80ea077c4a9345f88c21598fa04d70af6ac719294d88c397c2ccf4505d0b0101cc00249f0d62784332610100640400000882650100f77e";
        hex = "7e8300000c123456789012000108e58ebb38e6a5bc46e688bf8c7e";
        //        JTMessage msg = H2013(T0200JSATL12());
        T8300xy xy = new T8300xy();
        xy.setContent("去8楼F房");
        xy.setSign(0x08);
        JTMessage msg = H2013(xy);

        msg = decode(hex);
        hex = encode(msg);
    }

    private static String encode(JTMessage message) {
        System.out.println("====================================================================================\n");
        Explain explain = new Explain();
        ByteBuf buf = null;
        try {
            buf = coder.encode(message, explain);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String hex = ByteBufUtil.hexDump(buf);
//        System.out.println(message);
        System.out.println(hex);
        explain.println();
        return hex;
    }

    private static JTMessage decode(String hex) {
        System.out.println("====================================================================================\n");
        Explain explain = new Explain();
        JTMessage message = null;
        try {
            message = coder.decode(Unpooled.wrappedBuffer(ByteBufUtil.decodeHexDump(hex)), explain);
        } catch (Exception e) {
            e.printStackTrace();
        }
//        System.out.println(message);
//        System.out.println("~~~~~~~~~~~~~~~~~~~~~~");
        System.out.println(hex);
        explain.println();
        return message;
    }
}